<?php
namespace Home\Controller;
use Think\Controller;
use Think\Page;

class LiuyanController extends Controller {
    /**
     *
     */
    public function listAction(){

        $intPage = max(1,$_GET['p']);





        $liuyan = M("content"); // 实例化对象
        $arr = $liuyan->where()->order('id desc')->page($intPage.',3')->select();
        //print_r($arr);exit;
        $this->assign('arr',$arr);// 赋值数据集
        $count  = $liuyan->where()->count();// 查询满足要求的总记录数
        $Page   = new Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数

        //$Page->setConfig('header','<b>共 %TOTAL_ROW% 条记录</b>');
        $Page->setConfig('first','首页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','尾页');


        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        $huifu = M("huifu"); // 实例化对象
        foreach ($arr as $key => $value) { //获取回复
            $pid=$value['id'];
            $arr[$key]['huifu']=$huifu->where('pid='.$pid)->select();
        }
        $add=I('post.');
        if(!empty($add)){//print_r($add);exit;
            date_default_timezone_set('ASIA/Shanghai');
            $time=date('Y-m-d H:i:s',time());           //时间
            $add['time']=$time;

            //回复
            if(!empty($add['huifu'])){
                unset($add['savehuifu']);
                $add['name']=$_SESSION['user']['username'];
                $add['alias']=$_SESSION['user']['alias'];
                if(!empty($_SESSION['admin']['level'])){
                    $add['crut']=1;
                }
                $res=$huifu->data($add)->add();
                if($res>0){
                    $this->redirect('home/liuyan/list/p/1','', 3, '回复成功！等待跳转...');exit;
                }else{
                    $this->redirect('home/liuyan/list/p/1','',3, '回复失败！等待跳转...');exit;
                }

            }
            //回复编辑
            if(!empty($add['savehuifu'])){
                $str='id='.$add['id'];
                $data['huifu']=$add['savehuifu'];//print_r($data);exit;
                $res=$huifu->where($str)->save($data);

                if($res>0){
                    echo '1';exit;
                }else{
                    echo '2';exit;
                }
            }
            //留言
            $add['ip']=$_SERVER['SERVER_ADDR'];//ip

            //添加
            $result=$liuyan->data($add)->add();

            if($result>0){
                $this->success('留言成功!','list/p/1');exit;
            }else{
                $this->error('留言失败!','list/p/1');exit;
            }
        }
        $delid=I('get.delid');  //留言删除
        if(!empty($delid)){
            $res=$liuyan->where('id='.$delid)->delete(); // 删除id为delid的用户数据
            if($res>0){
                $huifu->where('pid='.$delid)->delete(); // 删除pid为delid的用户数据
                $this->redirect('home/liuyan/list/p/1','', 3, '删除成功！等待跳转...');
            }else{
                $this->redirect('home/liuyan/list/p/1','',3, '删除失败！等待跳转...');
            }
        }

        $huifu_id=I('get.delhuifu');//回复删除
        if(!empty($huifu_id)){
            $res=$huifu->where('id='.$huifu_id)->delete(); // 删除id为delid的用户数据
            if($res>0){
                $this->redirect('home/liuyan/list/p/1','', 3, '删除成功！等待跳转...');
            }else{
                $this->redirect('home/liuyan/list/p/1','',3, '删除失败！等待跳转...');
            }
        }
        $this->assign('arr',$arr);// 赋值输出
        $this->display('liuyanben');//加载视图
    }


}